import ConfigParser
import io
import os
import __main__

def parseConfig(configFile = None):
    if configFile == None:
        fileName, fileExtension = os.path.splitext(__main__.__file__)
        configFile = fileName + '.cfg'

    cfg = ConfigParser.RawConfigParser()
    try:
        cfg.read(configFile)
    except ConfigParser.MissingSectionHeaderError:
        fileData = open(configFile, 'r').read()
        fileData = '[__AutoSection]\n' + fileData
        cfg.readfp(io.BytesIO(fileData))

    config = {}

    for section in cfg.sections():
        for (key, value) in cfg.items(section):
            try:
                typedValue = int(value)
            except ValueError:
                try:
                    typedValue = float(value)
                except ValueError:
                    if value.lower() in ['true', 'yes', 'on']:
                        typedValue = True
                    elif value.lower() in ['false', 'no', 'off']:
                        typedValue = False
                    else:
                        typedValue = value

            if section == '__AutoSection':
               config[key] = typedValue
            else:
               config['%s:%s' % (section, key)] = typedValue

    return config





